package screen

import (
	"github.com/gdamore/tcell"
)

type PaintFunc struct {
	Func   func(interface{})
	Params interface{}
}

func (pf *PaintFunc) Run() {
	pf.Func(pf.Params)
}

type ParamsDrawBox struct {
	PointStartX int
	PointStartY int
	PointEndX   int
	PointEndY   int
	LineStyle   tcell.Style
}

func (s *Screen) ScreenPaintFuncAdd(PageCur int, Func PaintFunc) {
	s.PageCurPaintFuncList[PageCur] = append(s.PageCurPaintFuncList[PageCur], Func)
}

func (s *Screen) ScreenPaintFuncClear(PageCur int) {
	s.PageCurPaintFuncList[PageCur] = []PaintFunc{}
}

func (s *Screen) DrawBox(ParamsIf interface{}) {
	Params := ParamsIf.(ParamsDrawBox)
	if Params.PointEndY < Params.PointStartY {
		Params.PointStartY, Params.PointEndY = Params.PointEndY, Params.PointStartY
	}
	if Params.PointEndX < Params.PointStartX {
		Params.PointEndX, Params.PointStartX = Params.PointStartX, Params.PointEndX
	}
	s.ScreenObj.SetCell(Params.PointStartX, Params.PointStartY, Params.LineStyle, tcell.RuneHLine)
	s.ScreenObj.SetCell(Params.PointStartX, Params.PointEndY, Params.LineStyle, tcell.RuneHLine)
	for col := Params.PointStartX; col < Params.PointEndX-1; col++ {
		s.ScreenObj.SetCell(col, Params.PointStartY, Params.LineStyle, tcell.RuneHLine, tcell.RuneHLine)
		s.ScreenObj.SetCell(col, Params.PointEndY, Params.LineStyle, tcell.RuneHLine, tcell.RuneHLine)
	}
	s.ScreenObj.SetCell(Params.PointEndX-1, Params.PointStartY, Params.LineStyle, tcell.RuneHLine, tcell.RuneHLine)
	s.ScreenObj.SetCell(Params.PointEndX, Params.PointEndY, Params.LineStyle, tcell.RuneHLine, tcell.RuneHLine)

	for row := Params.PointStartY + 1; row < Params.PointEndY; row++ {
		s.ScreenObj.SetCell(Params.PointStartX, row, Params.LineStyle, tcell.RuneVLine, ' ')
		s.ScreenObj.SetCell(Params.PointEndX, row, Params.LineStyle, tcell.RuneVLine, ' ')
	}
	if Params.PointStartY != Params.PointEndY && Params.PointStartX != Params.PointEndX {
		// Only add corners if we need to
		s.ScreenObj.SetCell(Params.PointStartX, Params.PointStartY, Params.LineStyle, tcell.RuneULCorner, tcell.RuneHLine)
		s.ScreenObj.SetCell(Params.PointEndX-1, Params.PointStartY, Params.LineStyle, tcell.RuneHLine, tcell.RuneURCorner)
		s.ScreenObj.SetCell(Params.PointStartX, Params.PointEndY, Params.LineStyle, tcell.RuneLLCorner, tcell.RuneHLine)
		s.ScreenObj.SetCell(Params.PointEndX-1, Params.PointEndY, Params.LineStyle, tcell.RuneHLine, tcell.RuneLRCorner)
	}
	//for row := y1 + 1; row < y2; row++ {
	//	for col := x1 + 1; col < x2; col++ {
	//		s.ScreenObj.SetCell(col, row, style, r)
	//	}
	//}
}
